package org.example.demo1;

import com.google.gson.Gson;
import dao.cha;
import pojo.Student;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

@WebServlet(name = "chaxun", value = "/cha")

public class chaxun extends HelloServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse resp) throws IOException{
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html");
         cha cha = new cha();
         List<Student> list = cha.StduentAll();
//         创建hashmap
         HashMap<String, Object> has = new HashMap<>();
         has.put("code",200);
         has.put("msg","显示成功");
         has.put("data",list);
//         使用gson将其封装成json数据
         Gson gson = new Gson();
         String json = gson.toJson(has);
//         创建输出流输出
         PrintWriter writer = resp.getWriter();
         writer.println(json);
    }


}
